package com.eidetic.note;

import java.io.File;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import com.eidetic.util.INoteListener;

public class Note implements INote
{
	private long 	id;
	private	File	photo;
	private String	caption;
	private Date	dateCreated;
	private Date	dateDue;

	private Set<INoteListener> listeners;
	
	public Note(File photo)
	{
		this.photo 	= photo;
		listeners 	= new HashSet<INoteListener>();
	}
	
	@Override
	public File getPhoto()
	{
		return photo;
	}

	@Override
	public String getCaption()
	{
		return caption;
	}

	@Override
	public void setCaption(String caption)
	{
		this.caption = caption;
		broadcast();
	}

	@Override
	public Date getDateCreated()
	{
		return dateCreated;
	}

	@Override
	public Date getDateDue()
	{
		return dateDue;
	}

	@Override
	public void setDateDue(Date dateDue)
	{
		this.dateDue = dateDue;
		broadcast();
	}

	@Override
	public void registerListener(INoteListener listener)
	{
		listeners.add(listener);
	}

	@Override
	public void deleteListener(INoteListener listener)
	{
		listeners.remove(listener);
	}
	
	private void broadcast()
	{
		for (INoteListener listener : listeners)
			listener.onChanged(this);
	}

	@Override
	public long getId()
	{
		return id;
	}

	@Override
	public boolean setId(long id)
	{
		if (this.id != -1)
			return false;
		
		this.id = id;
		broadcast();
		
		return true;
	}

	@Override
	public boolean setDateCreated(Date dateCreated)
	{
		if (dateCreated != null)
			return false;
		
		this.dateCreated = dateCreated;
		broadcast();
		
		return true;
	}
}
